package com.wbl.train.member.controller;
import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.common.context.LoginMemberContext;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.member.dto.PassengerDto;
import com.wbl.train.common.member.entity.PassengerEntity;
import com.wbl.train.common.member.enums.PassengerTypeEnum;
import com.wbl.train.common.member.vo.PassengerVo;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.member.service.PassengerService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


/**
 * @author 王博纶
 * @since 2024-01-26
 * @description 乘车人
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/passenger")
@Tag(name = "乘车人控制器类",
        description = "PassengerController 乘车人 后端数据接口")
public class PassengerController {
    private final PassengerService passengerService;

    /**
     * 通过memberId查询 乘车人信息
     * @param passengerDto 乘车人Dto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<PassengerEntity>> getById(PassengerDto passengerDto) {
        // 会员查询 ，将对应的memberId塞进passengerDto中
        passengerDto.setMemberId(LoginMemberContext.getId());
        log.debug("passengerDto :{}",passengerDto);
        PageVo<PassengerEntity> pageVo = passengerService.queryPassenger(passengerDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 新增 乘车人
     * @param passenger 乘车人
     */
    @Operation(summary = "新增乘车人", description = "新增乘车人")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Valid PassengerEntity passenger) {
        return R.ok("保存成功", passengerService.insertAndUpdatePassenger(passenger));
    }

    /**
     * 通过id删除 乘车人
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除乘车人",
            description = "通过id删除乘车人")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(passengerService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前乘车人不存在");
        }
        return R.ok(passengerService.removeById(id));
    }

    /**
     * 获取乘车人枚举类数据
     * @return Result
     */
    @Operation(summary = "获取乘车人枚举类数据",
            description = "获取乘车人枚举类数据")
    @GetMapping("/enums")
    public R<List<HashMap<String,String>>> getEnum() {
        return R.ok(PassengerTypeEnum.getEnumList());
    }

    /**
     * 获取我的所有乘车人信息
     * @return 我的所有乘车人
     */
    @Operation(summary = "获取我的所有乘车人信息",
            description = "获取我的所有乘车人信息")
    @GetMapping("/query-mine")
    public R<List<PassengerVo>> getAllMinePassengers(){
        return R.ok("查询成功",passengerService.queryAllMinePassenger());
    }
}
